﻿@page "/"
@rendermode InteractiveWebAssembly
@inject IRepository PizzaStore
@inject IJSRuntime JSRuntime
@inject OrderState OrderState
@inject NavigationManager NavigationManager


<PageTitle>Blazing Pizzas</PageTitle>

<div class="main">
	<ul class="pizza-cards">
		@if (specials is not null)
		{
			@foreach (var special in specials)
			{
				<li @onclick="@(()=>OrderState.ShowConfigurePizzaDialog(special))" style="background-image: url('@special.ImageUrl')">
					<div class="pizza-info">
						<span class="title">@special.Name</span>
						@special.Description
						<span class="price">@special.GetFormattedBasePrice()</span>
					</div>
				</li>
			}
		}
	</ul>
</div>

<div class="sidebar">
	@if (OrderState.Order.Pizzas.Any())
	{
		<div class="order-contents">
			<h2>Your order</h2>

			@foreach (var configuredPizza in OrderState.Order.Pizzas)
			{
				<ConfiguredPizzaItem Pizza="configuredPizza" OnRemoved="@(() => RemovePizza(configuredPizza))" />
			}
		</div>
	}
	else
	{
		<div class="empty-cart">Choose a pizza<br>to get started</div>
	}

	<div class="order-total @(OrderState.Order.Pizzas.Any() ? "" : "hidden")">
		Total:
		<span class="total-price">@OrderState.Order.GetFormattedTotalPrice()</span>
		<a href="checkout" class="@(OrderState.Order.Pizzas.Count == 0 ? "btn btn-warning disabled" : "btn btn-warning")">
			Order >
		</a>
	</div>
</div>

<BlazingPizza.ComponentsLibrary.TemplatedDialog Show="OrderState.ShowingConfigureDialog">
	<ConfigurePizzaDialog Pizza="OrderState.ConfiguringPizza"
												OnCancel="OrderState.CancelConfigurePizzaDialog"
												OnConfirm="OrderState.ConfirmConfigurePizzaDialog" />
</BlazingPizza.ComponentsLibrary.TemplatedDialog>

@code {
	List<PizzaSpecial>? specials;

	protected override async Task OnInitializedAsync()
	{
		specials = await PizzaStore.GetSpecials();
		OrderState.JSRuntime = JSRuntime;
	}

	async Task RemovePizza(Pizza configuredPizza)
	{
		if (await JSRuntime.Confirm($"Remove {configuredPizza.Special?.Name} pizza from the order?"))
		{
			OrderState.RemoveConfiguredPizza(configuredPizza);
		}
	}

}